<?php

namespace App\Http\Controllers;

use App\Services\BaoliaoQueryService;
use Illuminate\Http\Request;

class BaoLiaoqueryController extends BaseController
{
    protected $queryService;
    
    public function __construct(BaoliaoQueryService $queryService)
    {
        $this->queryService = $queryService;
    }
    
    /**
     * 获取最新帖子
     */
    public function getLatest(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        
        $posts = $this->queryService->getLatestPosts($limit, $page);
        
        return response()->json([
            'data' => $posts,
            'meta' => [
                'current_page' => $page,
                'per_page' => $limit,
                'total' => count($posts) // 注意：这里需要实现总数统计
            ]
        ]);
    }
    
    /**
     * 获取最热帖子
     */
    public function getHottest(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        
        $posts = $this->queryService->getHottestPosts($limit, $page);
        //dd($posts);
        return response()->json([
            'data' => $posts,
            'meta' => [
                'current_page' => $page,
                'per_page' => $limit,
                'total' => count($posts) // 注意：这里需要实现总数统计
            ]
        ]);
    }
    
    /**
     * 获取综合排序帖子
     */
    public function getMixed(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $timeWeight = $request->get('time_weight', 0.5);
        $popularityWeight = $request->get('popularity_weight', 0.5);
        
        $posts = $this->queryService->getMixedPosts($limit, $page, $timeWeight, $popularityWeight);
        
        return response()->json([
            'data' => $posts,
            'meta' => [
                'current_page' => $page,
                'per_page' => $limit,
                'total' => count($posts) // 注意：这里需要实现总数统计
            ]
        ]);
    }
    
    /**
     * 搜索帖子
     */
    public function search(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $sortBy = $request->get('sort_by', 'latest');
        
        $conditions = [
            'category' => $request->get('category'),
            'brand' => $request->get('brand'),
            'min_price' => $request->get('min_price'),
            'max_price' => $request->get('max_price'),
            'start_date' => $request->get('start_date'),
            'end_date' => $request->get('end_date'),
            'keyword' => $request->get('keyword'),
        ];
        
        $posts = $this->queryService->searchPosts($conditions, $sortBy, $limit, $page);
        
        return response()->json([
            'data' => $posts,
            'meta' => [
                'current_page' => $page,
                'per_page' => $limit,
                'total' => count($posts) // 注意：这里需要实现总数统计
            ]
        ]);
    }
}
